<template>
  <div class="fillContain">
    <div>
      <el-form :inline="true" ref="add_data" v-model="search_data">
        <!-- 筛选 -->
        <el-form-item label="按照时间筛选"> 
          <el-date-picker
            v-model="search_data.startTime"
            type="datetime"
            placeholder="选择开始时间"
          >
          </el-date-picker>
          --
          <el-date-picker
            v-model="search_data.endTime"
            type="datetime"
            placeholder="选择结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item >
          <el-button type="primary" size="small" icon="search" @click="handleSearch()">筛选</el-button>
        </el-form-item>
        <!-- 添加 -->
        <el-form-item class="btnRight">
          <el-button type="primary" size="small" icon="view" v-if="user.identity =='manager'" @click="handleAdd()">添加</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表单 -->
    <div class="table_container">
      <el-table v-if="tableData.length > 0" :data="tableData" border style="width: 100%">
        <el-table-column type="index" label="序号" align="center" width="70"></el-table-column>
        <el-table-column prop="date" label="创建时间" align="center" width="250">
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.date }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="收支类型" align="center" width="150"></el-table-column>
        <el-table-column prop="desc" label="收支描述" align="center" width="180"></el-table-column>
        <el-table-column prop="income" label="收入" align="center" width="170">
          <template slot-scope="scope">
            <span style="color:#00d053">+ {{ scope.row.income }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="expend" label="支出" align="center" width="170">
          <template slot-scope="scope">
            <span style="color:#f56767">- {{ scope.row.expend }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="cash" label="账户现金" align="center" width="170">
          <template slot-scope="scope">
            <span style="color:#4db3ff">{{ scope.row.cash }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" align="center" width="220"></el-table-column>
        <el-table-column label="操作" prop="operation" align="center" width="326" v-if="user.identity =='manager'">
          <template slot-scope="scope">
            <el-button type="warning" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row>
        <el-col :span="24">
          <div class="pagination">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page.sync="paginations.page_index"
                :page-sizes="paginations.page_sizes"
                :page-size="paginations.page_size"
                :layout="paginations.layout"
                :total="paginations.total">
            </el-pagination>
          </div>
        </el-col>
      </el-row>
    </div>
    <!-- 弹框 -->
    <dialog-found :dialog="dialog" :formData="formData" @update="getProfile"></dialog-found>
  </div>
</template>

<script>
import DialogFound from "../components/DialogFound";
export default {
  name: "fundlist",
  components: {
    DialogFound
  },
  data() {
    return {
      //筛选数据初始化
      search_data: {
        satrtTime:"",
        endTime:"",
      },
      // 分页初始化
      paginations: {
        page_index: 1, //位于哪一页
        total: 20, //数据总数
        page_size: 5, //一页显示多少条
        page_sizes:[5, 10 , 20], //每页显示多少条
        layout: "total, sizes, prev, pager, next, jumper" //翻页属性
      },
      tableData: [], //为实现分页创建的容器
      allTableData: [], //显示数据创建的容器
      filterTableData: [], //为实现筛选创建的容器
      //弹框的初始化
      dialog: {
        show: false,
        title: "",
        option: "edit"
      },
      //子组件弹框表格数据初始化
      formData: {
        type: "",
        desc: "",
        income: "",
        expend: "",
        cash: "",
        remark: "",
        id: ""
      }
    }
  },
  // 从vuex中获取user的信息
  computed: {
    user() {
      return this.$store.getters.user
    }
  },
  //生命周期
  created() {
    this.getProfile();
  },
  methods: {
    // 获取表单数据
    getProfile() {

      //获取表格数据
      this.$axios
        .get("/api/profiles")
        .then(res => {
          this.allTableData = res.data
          this.filterTableData = res.data
           // 设置分页数据
          this.setPaginations()
        })
        .catch(err => {
          console.log(err);
        });
    },

    //数据编辑方法
    handleEdit(index, row) {
      //编辑
      this.dialog = {
        show: true,
        title: "修改资金信息",
        option: "edit"
      };
      this.formData = {
        type: row.type,
        desc: row.desc,
        income: row.income,
        expend: row.expend,
        cash: row.cash,
        remark: row.remark,
        id: row._id
      };
      // this.getProfile()
    },

    //数据删除方法
    handleDelete(index, row) {
      // 删除
      this.$axios.delete(`/api/profiles/del/${row._id}`).then(res => {
        this.$message("删除成功");
        this.getProfile();
      });
    },

    //数据添加方法
    handleAdd() {
      this.dialog = {
        show: true,
        title: "添加资金信息",
        option: "add"
      };
      this.formData = {
        type: "",
        desc: "",
        income: "",
        expend: "",
        cash: "",
        remark: "",
        id: ""
      };
    },

    // 实现切换当前页数据长度的方法
    handleSizeChange(page_size) {
      //切换size
      this.paginations.page_index = 1
      this.paginations.page_size = page_size
      this.tableData = this.allTableData.filter((item, index) => {
        return index < page_size
      })
    },

    //实现相应页面展示相应数据
    handleCurrentChange(page) {
      //获取当前页
      //获取当前页的索引
      let index = this.paginations.page_size * (page - 1)
      //获取总数
      let nums = this.paginations.page_size * page
      //容器
      let tables = []
      for(let i = index; i< nums; i++) {
        if (this.allTableData[i]) {
          tables.push(this.allTableData[i])
        }
        this.tableData = tables
      }
      

    },

    //分页初始化
    setPaginations() {
      // 总页数
      this.paginations.total = this.allTableData.length
      this.paginations.page_index = 1
      this.paginations.page_size = 5
      // 设置默认分页数据
      this.tableData = this.allTableData.filter((item, index) => {
        return index < this.paginations.page_size
      })
    },

    //筛选方法
    handleSearch() {
      //筛选
      if (!this.search_data.startTime || !this.search_data.endTime) {
        this.$message({
          type: "warning",
          message: "请选择时间区间"
        })
        this.getProfile()
        return
      }
      const stime = this.search_data.startTime.getTime()
      const etime = this.search_data.endTime.getTime()
      this.allTableData = this.filterTableData.filter(item => {
        let date = new Date(item.date)
        let time = date.getTime()
        return time >= stime && time <= etime;
      })
      // 分页数据
      this.setPaginations()
    }
  }
}
</script>

<style scoped>
.fillContain {
  width: 100%;
  height: 100%;
  padding: 16px;
  box-sizing: border-box;
}
.btnRight {
  float: right;
}
.pagination {
  text-align: right;
  margin-top: 10px;
}
</style>
